package com.hnevc.foodserver.controller;

import com.hnevc.foodserver.bean.Comments;
import com.hnevc.foodserver.bean.CommentsPageVo;
import com.hnevc.foodserver.bean.Foods;
import com.hnevc.foodserver.service.CommentsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comments")
@Api(tags = "评论接口")
public class CommentsController {
    @Autowired
    CommentsService commentsService;

    @PostMapping("/add")
    @ApiOperation(value = "发表评论", notes = "发表评论，请输入你的评论信息，需要内容和日期")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "comment", value = "评论", defaultValue = "1"),
            @ApiImplicitParam(name = "foodId", value = "食物ID", defaultValue = "1"),
            @ApiImplicitParam(name = "userId", value = "用户ID", defaultValue = "1"),
            @ApiImplicitParam(name = "postDate", value = "发表日期", defaultValue = "1999-09-09"),

    })
    public Map<String, Object> add(@RequestBody Comments comments) {
        Map<String, Object> map = new HashMap<>();
        Comments comments1 = commentsService.save(comments);

        if (comments1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", comments1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }


    @PostMapping("/add2")
    @ApiOperation(value = "发表评论", notes = "发表评论，请输入你的评论信息，需要内容和日期")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "comment", value = "评论", defaultValue = "1"),
            @ApiImplicitParam(name = "foodId", value = "食物ID", defaultValue = "1"),
            @ApiImplicitParam(name = "userId", value = "用户ID", defaultValue = "1"),
    })
    public Map<String, Object> add2(
            @RequestParam("comment") String comment
            ,@RequestParam("foodId") Integer foodId
            ,@RequestParam("userId") Integer userId
    ) {
        Comments comments = new Comments();
        comments.setComment(comment);
        comments.setFoodId(foodId);
        comments.setUserId(userId);
        comments.setPostDate(new Date());

        Map<String, Object> map = new HashMap<>();

        Comments comments1 = commentsService.save(comments);

        if (comments1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", comments1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }




    @PostMapping("/getCommentsByFoodId")
    @ApiOperation(value = "获取评论列表,根据条件获取评论列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "comment", value = "评论", defaultValue = "1"),
            @ApiImplicitParam(name = "foodId", value = "食物ID", defaultValue = "1"),
            @ApiImplicitParam(name = "userId", value = "用户ID", defaultValue = "1"),
            @ApiImplicitParam(name = "postDate", value = "发表日期", defaultValue = "1999-09-09"),
    })
    public Map<String, Object> list(@RequestBody Comments comments) {
        Map<String, Object> map = new HashMap<>();
        List<Comments> list = commentsService.getCommentsByFoodId(comments);
        if (list != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", list);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }



    @PostMapping("/getCommentsByFoodIdByPage")
    @ResponseBody
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "comment", value = "评论", defaultValue = "1"),
            @ApiImplicitParam(name = "foodId", value = "食物ID", defaultValue = "1"),
            @ApiImplicitParam(name = "userId", value = "用户ID", defaultValue = "1"),
            @ApiImplicitParam(name = "postDate", value = "发表日期", defaultValue = "1999-09-09"),
    })
    public Map<String, Object> listByPage(@RequestBody CommentsPageVo commentsPageVo) {
        Map<String, Object> map = new HashMap<>();

        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        PageRequest page = PageRequest.of(commentsPageVo.getPageIndex(), commentsPageVo.getPageSize(), sort);

        Page<Comments> page1 = commentsService.getCommentsByFoodIdByPage(page);
        if (page1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", page1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }

    @PostMapping("/findone")
    @ApiOperation(value = "获取一个评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "comment", value = "评论", defaultValue = "1"),
            @ApiImplicitParam(name = "foodId", value = "食物ID", defaultValue = "1"),
            @ApiImplicitParam(name = "userId", value = "用户ID", defaultValue = "1"),
            @ApiImplicitParam(name = "postDate", value = "发表日期", defaultValue = "1999-09-09"),
    })
    public Map<String, Object> findone(@RequestBody Comments comments) {
        Map<String, Object> map = new HashMap<>();
        Comments newComment = commentsService.findone(comments);
        if (newComment != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", newComment);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }

    @PostMapping("/list")
    @ApiOperation(value = "获取一个所有评论信息")
    public Map<String, Object> list() {
        Map<String, Object> map = new HashMap<>();
        List<Comments> list = commentsService.list();

        if (list != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", list);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }

    @PostMapping("/deleteone/{id}")
    @ResponseBody
    @ApiOperation(value = "删除一个评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", defaultValue = "1"),
    })
    public Map<String, Object> delete(@PathVariable("id") Integer id) {
        Map<String, Object> map = new HashMap<>();
        try {
            commentsService.deleteById(id);
            map.put("success", true);
            map.put("msg", "成功");
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", e.getMessage());
        }
        return map;
    }

    @PostMapping("/update")
    @ResponseBody
    @ApiOperation(value = "修改更新评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "comment", value = "评论", defaultValue = "1"),
            @ApiImplicitParam(name = "foodId", value = "食物ID", defaultValue = "1"),
            @ApiImplicitParam(name = "userId", value = "用户ID", defaultValue = "1"),
            @ApiImplicitParam(name = "postDate", value = "发表日期", defaultValue = "1999-09-09"),
    })

    public Map<String, Object> update(@RequestBody Comments comments) {
        Map<String, Object> map = new HashMap<>();
        try {
            Comments savedComment = commentsService.updateComment(comments);
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", savedComment);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", e.getMessage());
        }
        return map;
    }

}

